package com.bruan.d2_io;

import java.io.*;
import java.util.Set;

public class FileCopyTest {
    //复制视频的路径
    private static final  String SRC_FILE = "D:\\Desktop\\sucai1.mp4";
    private static final String DEST_FILE = "D:\\Desktop\\new_dir_test_idea\\";

    //复制到哪个地方
    public static void main(String[] args) {
//        copy01();
        copy02();
        copy03();
        copy04();
    }
    private static void copy01(){
        long startTime = System.currentTimeMillis();
        try(InputStream is = new FileInputStream(SRC_FILE);
            OutputStream os = new FileOutputStream(DEST_FILE + "1.mp4");) {
            int b;
            while ((b=is.read())!=-1){
                os.write(b);
            }
            System.out.println("复制完成");
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流，一个一个字节复制，耗时："+(endTime-startTime)/1000.0 + "s");
    }
    private static void copy02(){
        long startTime = System.currentTimeMillis();
        try(InputStream is = new FileInputStream(SRC_FILE);
            OutputStream os = new FileOutputStream(DEST_FILE + "2.mp4");) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len=is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
            System.out.println("复制完成");
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("低级字节流，字节数组复制，耗时："+(endTime-startTime)/1000.0 + "s");
    }
    private static void copy03(){
        long startTime = System.currentTimeMillis();
        try(InputStream is = new FileInputStream(SRC_FILE);
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = new FileOutputStream(DEST_FILE + "3.mp4");
            BufferedOutputStream bos = new BufferedOutputStream(os);
            ) {
            int b;
            while ((b=bis.read())!=-1){
                bos.write(b);
            }
            System.out.println("复制完成");
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲字节流，一个一个字节复制，耗时："+(endTime-startTime)/1000.0 + "s");
    }
    private static void copy04(){
        long startTime = System.currentTimeMillis();
        try(InputStream is = new FileInputStream(SRC_FILE);
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = new FileOutputStream(DEST_FILE + "4.mp4");
            BufferedOutputStream bos = new BufferedOutputStream(os);) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len=bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
            System.out.println("复制完成");
        }catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("缓冲字节流，字节数组复制，耗时："+(endTime-startTime)/1000.0 + "s");
    }
}
